//
// File: psdcenterdc.cpp
//
// MATLAB Coder version            : 5.4
// C/C++ source code generated on  : 25-Apr-2025 12:42:26
//

// Include Files
#include "psdcenterdc.h"
#include "colon.h"
#include "freqDomainHRV_data.h"
#include "freqDomainHRV_internal_types.h"
#include "rt_nonfinite.h"
#include "coder_array.h"
#include <cmath>
#include <string.h>

// Function Definitions
//
// Arguments    : ::coder::array<double, 2U> &Pxx
//                const ::coder::array<double, 1U> &F
//                ::coder::array<double, 2U> &Pxxc
//                const struct_T psdoptions
//                ::coder::array<double, 2U> &Pxx1
//                ::coder::array<double, 1U> &F1
//                ::coder::array<double, 2U> &Pxxc1
// Return Type  : void
//
namespace coder {
namespace signal {
namespace internal {
namespace spectral {
void psdcenterdc(::coder::array<double, 2U> &Pxx,
                 const ::coder::array<double, 1U> &F,
                 ::coder::array<double, 2U> &Pxxc, const struct_T psdoptions,
                 ::coder::array<double, 2U> &Pxx1,
                 ::coder::array<double, 1U> &F1,
                 ::coder::array<double, 2U> &Pxxc1)
{
  static const char b_cv[8]{'o', 'n', 'e', 's', 'i', 'd', 'e', 'd'};
  array<double, 2U> b_Pxx;
  array<double, 2U> b_Pxxc;
  array<double, 2U> idx;
  array<double, 2U> y;
  array<double, 1U> Pxx1_tmp;
  if (F.size(0) == 0) {
    int loop_ub;
    Pxx1.set_size(Pxx.size(0), Pxx.size(1));
    loop_ub = Pxx.size(0) * Pxx.size(1);
    for (int i{0}; i < loop_ub; i++) {
      Pxx1[i] = Pxx[i];
    }
    F1.set_size(0);
    Pxxc1.set_size(Pxxc.size(0), Pxxc.size(1));
    loop_ub = Pxxc.size(0) * Pxxc.size(1);
    for (int i{0}; i < loop_ub; i++) {
      Pxxc1[i] = Pxxc[i];
    }
  } else {
    double Fs;
    double r;
    int i;
    int i1;
    int kstr;
    int loop_ub;
    boolean_T even_nfft;
    boolean_T isonesided;
    if (std::isnan(psdoptions.nfft) || std::isinf(psdoptions.nfft)) {
      r = rtNaN;
    } else if (psdoptions.nfft == 0.0) {
      r = 0.0;
    } else {
      r = std::fmod(psdoptions.nfft, 2.0);
      if (r == 0.0) {
        r = 0.0;
      } else if (psdoptions.nfft < 0.0) {
        r += 2.0;
      }
    }
    even_nfft = (r == 0.0);
    isonesided = false;
    kstr = 0;
    int exitg1;
    do {
      exitg1 = 0;
      if (kstr < 8) {
        if (cv[static_cast<int>(psdoptions.range[kstr])] !=
            cv[static_cast<int>(b_cv[kstr])]) {
          exitg1 = 1;
        } else {
          kstr++;
        }
      } else {
        isonesided = true;
        exitg1 = 1;
      }
    } while (exitg1 == 0);
    if (isonesided) {
      if (even_nfft) {
        int i2;
        if (Pxx.size(0) - 1 < 2) {
          i = 0;
          i1 = 0;
        } else {
          i = 1;
          i1 = Pxx.size(0) - 1;
        }
        i2 = (Pxx.size(0) - 1 >= 2);
        kstr = Pxx.size(1) - 1;
        loop_ub = i1 - i;
        b_Pxx.set_size(loop_ub, Pxx.size(1));
        for (i1 = 0; i1 <= kstr; i1++) {
          for (int i3{0}; i3 < loop_ub; i3++) {
            b_Pxx[i3] = Pxx[i + i3] / 2.0;
          }
        }
        loop_ub = b_Pxx.size(1);
        for (i = 0; i < loop_ub; i++) {
          kstr = b_Pxx.size(0);
          for (i1 = 0; i1 < kstr; i1++) {
            Pxx[i2 + i1] = b_Pxx[i1];
          }
        }
        if ((Pxxc.size(0) != 0) && (Pxxc.size(1) != 0)) {
          if (Pxxc.size(0) - 1 < 2) {
            i = 0;
            i1 = 0;
          } else {
            i = 1;
            i1 = Pxxc.size(0) - 1;
          }
          i2 = (Pxxc.size(0) - 1 >= 2);
          kstr = Pxxc.size(1) - 1;
          loop_ub = i1 - i;
          b_Pxx.set_size(loop_ub, Pxxc.size(1));
          for (i1 = 0; i1 <= kstr; i1++) {
            for (int i3{0}; i3 < loop_ub; i3++) {
              b_Pxx[i3 + b_Pxx.size(0) * i1] =
                  Pxxc[(i + i3) + Pxxc.size(0) * i1] / 2.0;
            }
          }
          loop_ub = b_Pxx.size(1);
          kstr = b_Pxx.size(0);
          for (i = 0; i < loop_ub; i++) {
            for (i1 = 0; i1 < kstr; i1++) {
              Pxxc[(i2 + i1) + Pxxc.size(0) * i] =
                  b_Pxx[i1 + b_Pxx.size(0) * i];
            }
          }
        }
        if (F.size(0) - 1 < 2) {
          y.set_size(1, 0);
        } else {
          y.set_size(1, F.size(0) - 2);
          loop_ub = F.size(0) - 3;
          for (i = 0; i <= loop_ub; i++) {
            y[i] =
                (static_cast<double>(F.size(0)) - 1.0) - static_cast<double>(i);
          }
        }
        idx.set_size(1, F.size(0) + y.size(1));
        loop_ub = y.size(1);
        for (i = 0; i < loop_ub; i++) {
          idx[i] = y[i];
        }
        loop_ub = F.size(0) - 1;
        for (i = 0; i <= loop_ub; i++) {
          idx[i + y.size(1)] = static_cast<double>(i) + 1.0;
        }
      } else {
        int i2;
        if (Pxx.size(0) < 2) {
          i = 0;
          i1 = 0;
          i2 = 0;
        } else {
          i = 1;
          i1 = Pxx.size(0);
          i2 = 1;
        }
        kstr = Pxx.size(1) - 1;
        loop_ub = i1 - i;
        b_Pxx.set_size(loop_ub, Pxx.size(1));
        for (i1 = 0; i1 <= kstr; i1++) {
          for (int i3{0}; i3 < loop_ub; i3++) {
            b_Pxx[i3] = Pxx[i + i3] / 2.0;
          }
        }
        loop_ub = b_Pxx.size(1);
        for (i = 0; i < loop_ub; i++) {
          kstr = b_Pxx.size(0);
          for (i1 = 0; i1 < kstr; i1++) {
            Pxx[i2 + i1] = b_Pxx[i1];
          }
        }
        if ((Pxxc.size(0) != 0) && (Pxxc.size(1) != 0)) {
          if (Pxxc.size(0) < 2) {
            i = 0;
            i1 = 0;
            i2 = 0;
          } else {
            i = 1;
            i1 = Pxxc.size(0);
            i2 = 1;
          }
          kstr = Pxxc.size(1) - 1;
          loop_ub = i1 - i;
          b_Pxxc.set_size(loop_ub, Pxxc.size(1));
          for (i1 = 0; i1 <= kstr; i1++) {
            for (int i3{0}; i3 < loop_ub; i3++) {
              b_Pxxc[i3 + b_Pxxc.size(0) * i1] =
                  Pxxc[(i + i3) + Pxxc.size(0) * i1] / 2.0;
            }
          }
          loop_ub = b_Pxxc.size(1);
          kstr = b_Pxxc.size(0);
          for (i = 0; i < loop_ub; i++) {
            for (i1 = 0; i1 < kstr; i1++) {
              Pxxc[(i2 + i1) + Pxxc.size(0) * i] =
                  b_Pxxc[i1 + b_Pxxc.size(0) * i];
            }
          }
        }
        if (F.size(0) < 2) {
          y.set_size(1, 0);
        } else {
          y.set_size(1, F.size(0) - 1);
          loop_ub = F.size(0) - 2;
          for (i = 0; i <= loop_ub; i++) {
            y[i] = static_cast<double>(F.size(0)) - static_cast<double>(i);
          }
        }
        idx.set_size(1, F.size(0) + y.size(1));
        loop_ub = y.size(1);
        for (i = 0; i < loop_ub; i++) {
          idx[i] = y[i];
        }
        loop_ub = F.size(0) - 1;
        for (i = 0; i <= loop_ub; i++) {
          idx[i + y.size(1)] = static_cast<double>(i) + 1.0;
        }
      }
    } else if (even_nfft) {
      r = static_cast<double>(F.size(0)) / 2.0 + 2.0;
      if (F.size(0) < r) {
        y.set_size(1, 0);
      } else if (std::floor(r) == r) {
        y.set_size(1, (F.size(0) - static_cast<int>(r)) + 1);
        loop_ub = F.size(0) - static_cast<int>(r);
        for (i = 0; i <= loop_ub; i++) {
          y[i] = r + static_cast<double>(i);
        }
      } else {
        b_eml_float_colon(r, static_cast<double>(F.size(0)), y);
      }
      r = static_cast<double>(F.size(0)) / 2.0 + 1.0;
      idx.set_size(1, (y.size(1) + static_cast<int>(r - 1.0)) + 1);
      loop_ub = y.size(1);
      for (i = 0; i < loop_ub; i++) {
        idx[i] = y[i];
      }
      loop_ub = static_cast<int>(r - 1.0);
      for (i = 0; i <= loop_ub; i++) {
        idx[i + y.size(1)] = static_cast<double>(i) + 1.0;
      }
    } else {
      r = (static_cast<double>(F.size(0)) + 1.0) / 2.0 + 1.0;
      if (F.size(0) < r) {
        y.set_size(1, 0);
      } else if (std::floor(r) == r) {
        y.set_size(1, (F.size(0) - static_cast<int>(r)) + 1);
        loop_ub = F.size(0) - static_cast<int>(r);
        for (i = 0; i <= loop_ub; i++) {
          y[i] = r + static_cast<double>(i);
        }
      } else {
        b_eml_float_colon(r, static_cast<double>(F.size(0)), y);
      }
      r = (static_cast<double>(F.size(0)) + 1.0) / 2.0;
      idx.set_size(1, (y.size(1) + static_cast<int>(r - 1.0)) + 1);
      loop_ub = y.size(1);
      for (i = 0; i < loop_ub; i++) {
        idx[i] = y[i];
      }
      loop_ub = static_cast<int>(r - 1.0);
      for (i = 0; i <= loop_ub; i++) {
        idx[i + y.size(1)] = static_cast<double>(i) + 1.0;
      }
    }
    Pxx1_tmp.set_size(idx.size(1));
    loop_ub = idx.size(1);
    for (i = 0; i < loop_ub; i++) {
      Pxx1_tmp[i] = idx[i];
    }
    loop_ub = Pxx.size(1);
    Pxx1.set_size(Pxx1_tmp.size(0), Pxx.size(1));
    for (i = 0; i < loop_ub; i++) {
      kstr = Pxx1_tmp.size(0);
      for (i1 = 0; i1 < kstr; i1++) {
        Pxx1[i1] = Pxx[static_cast<int>(Pxx1_tmp[i1]) - 1];
      }
    }
    F1.set_size(idx.size(1));
    loop_ub = idx.size(1);
    for (i = 0; i < loop_ub; i++) {
      F1[i] = F[static_cast<int>(idx[i]) - 1];
    }
    if ((Pxxc.size(0) != 0) && (Pxxc.size(1) != 0)) {
      loop_ub = Pxxc.size(1);
      Pxxc1.set_size(Pxx1_tmp.size(0), Pxxc.size(1));
      kstr = Pxx1_tmp.size(0);
      for (i = 0; i < loop_ub; i++) {
        for (i1 = 0; i1 < kstr; i1++) {
          Pxxc1[i1 + Pxxc1.size(0) * i] =
              Pxxc[(static_cast<int>(Pxx1_tmp[i1]) + Pxxc.size(0) * i) - 1];
        }
      }
    } else {
      Pxxc1.set_size(Pxxc.size(0), Pxxc.size(1));
      loop_ub = Pxxc.size(0) * Pxxc.size(1);
      for (i = 0; i < loop_ub; i++) {
        Pxxc1[i] = Pxxc[i];
      }
    }
    if (std::isnan(psdoptions.Fs)) {
      Fs = 6.2831853071795862;
    } else {
      Fs = psdoptions.Fs;
    }
    if (isonesided) {
      i = idx.size(1) - F.size(0);
      if (i < 1) {
        loop_ub = 0;
      } else {
        loop_ub = i;
      }
      y.set_size(1, loop_ub);
      for (i = 0; i < loop_ub; i++) {
        y[i] = -F1[i];
      }
      loop_ub = y.size(1);
      for (i = 0; i < loop_ub; i++) {
        F1[i] = y[i];
      }
    } else if (even_nfft) {
      r = static_cast<double>(F.size(0)) / 2.0 - 1.0;
      if (r < 1.0) {
        loop_ub = 0;
      } else {
        loop_ub = static_cast<int>(r);
      }
      y.set_size(1, loop_ub);
      for (i = 0; i < loop_ub; i++) {
        y[i] = F1[i] - Fs;
      }
      loop_ub = y.size(1);
      for (i = 0; i < loop_ub; i++) {
        F1[i] = y[i];
      }
    } else {
      r = (static_cast<double>(F.size(0)) - 1.0) / 2.0;
      if (r < 1.0) {
        loop_ub = 0;
      } else {
        loop_ub = static_cast<int>(r);
      }
      y.set_size(1, loop_ub);
      for (i = 0; i < loop_ub; i++) {
        y[i] = F1[i] - Fs;
      }
      loop_ub = y.size(1);
      for (i = 0; i < loop_ub; i++) {
        F1[i] = y[i];
      }
    }
  }
}

} // namespace spectral
} // namespace internal
} // namespace signal
} // namespace coder

//
// File trailer for psdcenterdc.cpp
//
// [EOF]
//
